﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace frmCheckStock
{
    public class CategoryBroker
    {
        SqlConnection connection;
        SqlCommand cmInsert;
        SqlCommand command;
        SqlTransaction transaction;
        //SqlDataAdapter adapter;

        public CategoryBroker()
        {
            string conS = "data source=(local);Initial catalog=LogicUniversityT2;integrated security=SSPI";
            connection = new SqlConnection(conS);
            cmInsert = new SqlCommand();

            cmInsert.CommandText = "Insert into Category(CategoryID,CategoryName) VALUES(@CategoryID,@CategoryName)";
            cmInsert.Connection = connection;

        }

        #region transaction
        public void StartTransaction()
        {
            if (connection.State == ConnectionState.Closed)
                connection.Open();

            transaction = connection.BeginTransaction();
        }
        public void CommitTransaction()
        {
            if (transaction != null)
                transaction.Commit();

            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
        public void RollbackTransaction()
        {
            if (transaction != null)
                transaction.Rollback();

            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
        #endregion

        #region Insert

        public void Category_Insert(String categoryid, String categoryname)
        {
            command = new SqlCommand("Category_Insert", connection, transaction);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@CategoryID", SqlDbType.NVarChar).Value = categoryid;
            command.Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value = categoryname;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

        }
        public bool Category_Insertt(CategoryDataAcess info)
        {
            try
            {
                StartTransaction();
                Category_Insert(info.CategoryID, info.CategoryName);
                CommitTransaction();
                return true;
            }
            catch
            {
                RollbackTransaction();
                return false;
            }
        }

        #endregion

        #region AutoGenerate
        public IDataReader AutoGenerateCategory()
        {
            command = new SqlCommand("Category_CodeGen", connection);
            command.CommandType = CommandType.StoredProcedure;

            connection.Open();
            return command.ExecuteReader(CommandBehavior.CloseConnection);


        }

        #endregion
       
         #region AutoGen

        public string AutoGenCategoryID()
        {
            //CategoryController controller = new CategoryController();
            IDataReader Reader = AutoGenerateCategory();

            string CategoryID = "";

            if (Reader.Read())
            {
                CategoryID = Convert.ToString(Reader["Maxcategoryid"]);
                Reader.Close();
            }

            if (CategoryID == "")
            {
                CategoryID = "C-001";
                return CategoryID;
            }
            else
            {
                CategoryID = CategoryID.Substring(2);
                int MaxCategoryID = Convert.ToInt32(CategoryID) + 1;
                CategoryID = string.Format("C-{0:000}", MaxCategoryID);
                return CategoryID;
            }
        }

        #endregion
    }
}
